<template>
  <div class="container">
    <div class="app-container" style="padding: 20px;">
      <el-button type="primary" @click="add">添加角色</el-button>

      <!-- :data 数据源 -->
      <el-table :data="tableData" border style="margin: 20px 0" size="mini">
        <!-- type="index" 单独展示一列序号 -->
        <el-table-column type="index" label="序号" width="100" align="center" />

        <!-- table只有列的概念  每一列必须有一个指定的字段名 -->
        <!-- <el-table-column prop="name" label="角色" width="100" /> -->
        <!-- 行内编辑的核心在于你理解 table-column的默认插槽可以决定展示内容 -->
        <el-table-column prop="name" label="角色" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <!-- 绑定临时数据 临时数据随便你折腾  我有备份 真正的数据在row.name -->
            <el-input v-else v-model="row.temp.name" size="mini" />
          </template>
        </el-table-column>

        <!-- state是1 显示 启用 否则未启用 -->
        <!-- <el-table-column prop="state" label="是否启用" width="100" /> -->
        <el-table-column label="是否启用" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.state === 1 ? '启用' : '未启用' }}</span>
            <el-switch v-else v-model="row.temp.state" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>

        <el-table-column prop="description" label="描述">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <el-input v-else v-model="row.temp.description" type="textarea" size="mini" />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <!-- 通过默认插槽指定结构 -->
          <!-- <a href="https://www.baidu.com">百度</a> -->
          <!-- 文字按钮 -->
          <template #default="{row}">
            <!-- {{ row }} -->
            <!-- obj是这个组件的作者在封装el-table-column组件时，传递给默认插槽的参数 -->
            <div v-if="!row.isEdit">
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="row.isEdit=true">编辑</el-button>
              <!-- 希望删除时 先弹气泡确认 -->
              <el-popconfirm title="你确定???" @onConfirm="del(row)">
                <template #reference>
                  <!-- 固定显示的结构 -->
                  <el-button type="text">删除</el-button>
                </template>
              </el-popconfirm>

            </div>
            <div v-else>
              <el-button size="mini" type="primary" @click="edit(row)">确定</el-button>
              <el-button size="mini" @click="cancel(row)">取消</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <template>
        <el-pagination
          style="width: 100%; height: 50px; margin-top: 20px; justify-content: end; display: flex"
          :current-page="page"
          :page-sizes="[10,20,30]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </template>
      <el-dialog :visible.sync="showDialog" @close="addCancel">
        <el-form ref="addForm" :model="obj" :rules="rules" label-width="200px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="obj.name" />
          </el-form-item>
          <el-form-item prop="state">
            <el-switch
              v-model="obj.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
          <el-form-item type="textarea" label="角色描述" prop="description">
            <el-input v-model="obj.description" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queren">确认</el-button>
            <el-button type="primary" @click="addCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, addRoles, updateRole, deleteRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      isEdit: false,
      showDialog: false,
      tableData: [],
      total: 0,
      page: 1,
      pagesize: 5,
      obj: {
        name: '',
        state: 1,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    async getRoles() {
      const res = await getRoles(this.page, this.pagesize)
      this.tableData = res.rows
      this.total = res.total
      // $set
      this.tableData.forEach((item) => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'temp', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    handleCurrentChange(value) {
      this.page = value
      this.getRoles()
    },
    handleSizeChange(value) {
      this.pagesize = value
      this.getRoles()
    },
    add() {
      this.showDialog = true
    },
    queren() {
      this.$refs.addForm.validate(async isOk => {
        if (isOk) {
          await addRoles(this.obj)
          this.$message.success('添加成功')
          this.getRoles()
          this.addCancel()
        }
      })
    },
    addCancel() {
      this.showDialog = false
      this.$refs.addForm.resetFields()
      this.objs = {
        name: '',
        state: 0,
        description: ''
      }
    },

    // 删除
    async del(row) {
      await deleteRole(row.id)
      this.$message.success('删除成功')
      this.isEdit = false
      this.getRoles()
      if (this.page > 1 && this.tableData.length === 1) this.page--
    },
    // 编辑
    async edit(row) {
      await updateRole({
        ...row.temp,
        id: row.id
      })
      this.$message.success('编辑成功')
      this.getRoles()
    },
    // 取消
    cancel(row) {
      row.isEdit = false
      row.temp.name = row.name
      row.temp.description = row.description
      row.temp.state = row.state
    }
  }
}
</script>
